import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import Father from '@/components/sync/Father.vue'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import(/* webpackChunkName:"login"*/'@/views/login/index'),
    hidden: true
  },
  {
    path: '/father',
    component: Father,
    hidden: true
  },
  {
    path: '/excel',
    component: () => import('@/views/excel/index.vue'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  }

]

const req = require.context('./modules', false, /\.js$/)
const mModule = req.keys().map(req)
console.log('mModule', mModule)
// 动态路由
export const doRoutes = mModule.map(item => item.default)
// console.log(doRoutes)

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // routes: [...constantRoutes, ...doRoutes]
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
